<template>
  <s-modal
    title="新增"
    :visible="visible"
    width="800px"
    ok-text="保存"
    :confirm-loading="confirmLoading"
    :closable="!confirmLoading"
    @ok="save()"
    @cancel="
      () => {
        if (!confirmLoading) {
          visible = false
        }
      }
    "
  >
    <div style="height: 400px">
      <s-scroll>
        <s-form ref="form" :value="formValue">
          <s-form-item
            label="应用名称"
            size="middle"
            name="name"
            :colon="true"
            :display-title="true"
            :rules="[
              { whitespace: true, required: true, message: '应用名称不能为空' },
              { max: 20, message: '最长为20位' }
            ]"
          >
            <s-input v-model="formValue.name" :allow-clear="true" placeholder="请输入应用名称" />
          </s-form-item>
          <s-form-item
            label="编码"
            size="middle"
            name="code"
            :colon="true"
            :display-title="true"
            :rules="[
              { whitespace: true, required: true, message: '编码不能为空' },
              { max: 30, message: '最长为30位' },
              { pattern: '^([A-Za-z0-9]|_)+$', message: '编码格式不正确' }
            ]"
          >
            <s-input v-model="formValue.code" :allow-clear="true" placeholder="请输入编码" />
          </s-form-item>
          <s-form-item
            label="所属类别"
            size="middle"
            name="type"
            :colon="true"
            :display-title="true"
          >
            <s-select
              v-model="formValue.type"
              :allow-clear="true"
              placeholder="请输入所属类别"
              :show-search="true"
              code="APP_TYPE"
            />
          </s-form-item>
          <s-form-item
            label="访问地址"
            size="middle"
            name="accessPath"
            :colon="true"
            :display-title="true"
            :rules="[
              { whitespace: true, required: true, message: '访问地址不能为空' },
              { max: 255, message: '最长为255位' }
            ]"
          >
            <s-input
              v-model="formValue.accessPath"
              :allow-clear="true"
              placeholder="请输入访问地址"
            />
          </s-form-item>
          <s-form-item
            label="应用概况"
            size="big"
            name="appDesc"
            :colon="true"
            :display-title="true"
            :rules="[{ max: 2000, message: '最长为2000位' }]"
          >
            <s-text-area
              v-model="formValue.appDesc"
              :allow-clear="true"
              placeholder="请输入应用概况"
              :row-num="4"
            />
          </s-form-item>
          <s-form-item
            label="应用图标"
            size="middle"
            name="icon"
            :colon="true"
            :display-title="true"
          >
            <s-img v-model="formValue.icon" :allow-clear="true" :max="1" />
          </s-form-item>
          <s-form-item
            label="排序号"
            size="middle"
            name="orderNum"
            :rules="[{ required: true, message: '排序号不能为空' }]"
          >
            <s-input-number v-model="formValue.orderNum" placeholder="请输入排序号" />
          </s-form-item>
        </s-form>
      </s-scroll>
    </div>
  </s-modal>
</template>

<script>
export default {
  name: 'Add',
  emits: ['change'],
  data() {
    return {
      formValue: {},
      confirmLoading: false,
      visible: false
    }
  },
  methods: {
    open() {
      this.formValue = {}
      this.visible = true
    },
    save() {
      const _this = this
      const form = this.$refs.form
      form.validate((values, fullValue) => {
        this.confirmLoading = true
        this.$api
          .PUT('/realm/app/insert', values)
          .then((res) => {
            this.confirmLoading = false
            this.visible = false
            this.$emit('success')
            this.$message.info(res.msg)
          })
          .catch(() => {
            this.confirmLoading = false
          })
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
